home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / Apps / ArchiveUtils / nx_arc / arcext.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-20  |  5.1 KB  |  212 lines

  1. /*
  2.  * $Log:    arcext.c,v $
  3.  * Revision 1.2  88/04/11  17:59:09  hyc
  4.  * re-synch with MTS, formatting
  5.  * 
  6.  * Revision 1.1  88/04/11  17:56:12  hyc
  7.  * Initial revision
  8.  * 
  9.  * Revision 1.4  87/08/13  17:03:21  hyc
  10.  * Run thru the indent program...
  11.  *  Revision 1.3  87/07/21  11:39:59  hyc minor
  12.  * fixups
  13.  * 
  14.  * Revision 1.2  87/07/21  07:32:17  hyc added BSD goodies
  15.  * 
  16.  */
  17.  
  18. /*
  19.  * ARC - Archive utility - ARCEXT
  20.  * 
  21.  * Version 2.19, created on 10/24/86 at 14:53:32
  22.  * 
  23.  * (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  24.  * 
  25.  * By:  Thom Henderson
  26.  * 
  27.  * Description: This file contains the routines used to extract files from an
  28.  * archive.
  29.  * 
  30.  * Language: Computer Innovations Optimizing C86
  31.  */
  32. #include <stdio.h>
  33. #include "arc.h"
  34.  
  35. INT
  36. extarc(num, arg, prt)        /* extract files from archive */
  37.     INT             num;    /* number of arguments */
  38.     char           *arg[];    /* pointers to arguments */
  39. INT             prt;        /* true if printing */
  40. {
  41.     struct heads    hdr;    /* file header */
  42.     INT             save;    /* true to save current file */
  43.     INT             did[25];/* true when argument was used */
  44.     char           *i, *rindex();    /* string index */
  45.     char          **name, *malloc();    /* name pointer list,
  46.                          * allocator */
  47.     INT             n;    /* index */
  48.     INT             extfile();
  49.  
  50.     name = (char **) malloc(num * sizeof(char *));    /* get storage for name
  51.                              * pointers */
  52.  
  53.     for (n = 0; n < num; n++) {    /* for each argument */
  54.         did[n] = 0;    /* reset usage flag */
  55. #ifndef MTS
  56.         if (!(i = rindex(arg[n], '\\')))    /* find start of name */
  57.             if (!(i = rindex(arg[n], '/')))
  58.                 if (!(i = rindex(arg[n], ':')))
  59.                     i = arg[n] - 1;
  60. #else
  61.         if (!(i = rindex(arg[n], sepchr[0])))
  62.             if (arg[n][0] != tmpchr[0])
  63.                 i = arg[n] - 1;
  64.             else
  65.                 i = arg[n];
  66. #endif
  67.         name[n] = i + 1;
  68.     }
  69.  
  70.  
  71.     openarc(0);        /* open archive for reading */
  72.  
  73.     if (num) {        /* if files were named */
  74.         while (readhdr(&hdr, arc)) {    /* while more files to check */
  75.             save = 0;    /* reset save flag */
  76.             for (n = 0; n < num; n++) {    /* for each template
  77.                              * given */
  78.                 if (match(hdr.name, name[n])) {
  79.                     save = 1;    /* turn on save flag */
  80.                     did[n] = 1;    /* turn on usage flag */
  81.                     break;    /* stop looking */
  82.                 }
  83.             }
  84.  
  85.             if (save)    /* extract if desired, else skip */
  86.                 extfile(&hdr, arg[n], prt);
  87.             else
  88.                 fseek(arc, hdr.size, 1);
  89.         }
  90.     } else
  91.         while (readhdr(&hdr, arc))    /* else extract all files */
  92.             extfile(&hdr, "", prt);
  93.  
  94.     closearc(0);        /* close archive after reading */
  95.  
  96.     if (note) {
  97.         for (n = 0; n < num; n++) {    /* report unused args */
  98.             if (!did[n]) {
  99.                 printf("File not found: %s\n", arg[n]);
  100.                 nerrs++;
  101.             }
  102.         }
  103.     }
  104.     free(name);
  105. }
  106.  
  107. INT
  108. extfile(hdr, path, prt)        /* extract a file */
  109.     struct heads   *hdr;    /* pointer to header data */
  110.     char           *path;    /* pointer to path name */
  111.     INT             prt;    /* true if printing */
  112. {
  113.     FILE           *f, *fopen();    /* extracted file, opener */
  114.     char            buf[100];    /* input buffer */
  115.     char            fix[100];    /* fixed name buffer */
  116.     char           *i, *rindex();    /* string index */
  117.  
  118.     if (prt) {        /* printing is much easier */
  119.         unpack(arc, stdout, hdr);    /* unpack file from archive */
  120.         printf("\f");    /* eject the form */
  121.         return;        /* see? I told you! */
  122.     }
  123.     strcpy(fix, path);    /* note path name template */
  124. #ifndef MTS
  125.     if (!(i = rindex(fix, '\\')))    /* find start of name */
  126.         if (!(i = rindex(fix, '/')))
  127.             if (!(i = rindex(fix, ':')))
  128.                 i = fix - 1;
  129. #else
  130.     if (!(i = rindex(fix, sepchr[0])))
  131.         if (fix[0] != tmpchr[0])
  132.             i = fix - 1;
  133.         else
  134.             i = fix;
  135. #endif
  136.     strcpy(i + 1, hdr->name);    /* replace template with name */
  137.  
  138.     if (note)
  139.         printf("Extracting file: %s\n", fix);
  140.  
  141.     if (warn && !overlay) {
  142.         if (f = fopen(fix, "r")) {    /* see if it exists */
  143. #ifdef MTS
  144.             if ((buf[0] = fgetc(f)) != EOF) {    /* kludge for temp files */
  145. #endif
  146.                 fclose(f);
  147.                 printf("WARNING: File %s already exists!", fix);
  148.                 fflush(stdout);
  149.                 while (1) {
  150.                     printf("  Overwrite it (y/n)? ");
  151.                     fflush(stdout);
  152.                     fgets(buf, 100, stdin);
  153.                     *buf = toupper(*buf);
  154.                     if (*buf == 'Y' || *buf == 'N')
  155.                         break;
  156.                 }
  157.                 if (*buf == 'N') {
  158.                     printf("%s not extracted.\n", fix);
  159.                     fseek(arc, hdr->size, 1);
  160.                     return;
  161.                 }
  162. #ifdef MTS
  163.             }
  164. #endif
  165.         }
  166.     }
  167. #ifndef MTS
  168.     if (!(f = fopen(fix, "w"))) {
  169. #else
  170.     {
  171.         fortran         create();
  172.         char            c_name[256];
  173.         struct crsize {
  174.             short           maxsize, cursize;
  175.         }               c_size;
  176.         char            c_vol[6];
  177.         int             c_type;
  178.         strcpy(c_name, fix);
  179.         strcat(c_name, " ");
  180.         c_size.maxsize = 0;
  181.         c_size.cursize = hdr->length / 4096 + 1;
  182.         memset(c_vol, 0, sizeof(c_vol));
  183.         c_type = 0x100;
  184.         create(c_name, &c_size, c_vol, &c_type);
  185.     }
  186.     if (image) {
  187.         f = fopen(fix, "wb");
  188.         fseek(f, 0, 0);
  189.     } else
  190.         f = fopen(fix, "w");
  191.     if (!f) {
  192. #endif
  193.         if (warn) {
  194.             printf("Cannot create %s\n", fix);
  195.             nerrs++;
  196.         }
  197.         fseek(arc, hdr->size, 1);
  198.         return;
  199.     }
  200.     /* now unpack the file */
  201.  
  202.     unpack(arc, f, hdr);    /* unpack file from archive */
  203. #ifdef MSDOS
  204.     setstamp(f, hdr->date, hdr->time);    /* set the proper date/time
  205.                          * stamp */
  206. #endif
  207.     fclose(f);        /* all done writing to file */
  208. #ifdef BSD
  209.     setstamp(fix, hdr->date, hdr->time);    /* use filename for BSD stamp */
  210. #endif
  211. }
  212.